using System;
using System.Threading;

namespace SBPweb.Forge.Common.Diagnostics
{
    /// <summary>
    /// Watchdog
    /// </summary>
	public class Watchdog
    {
        private volatile Thread backgroundThread;
        private volatile int period = 60000;

		/// <summary>
		/// Period
		/// </summary>
		public int Period
        {
            get
            {
                return period;
            }
            set
            {
                period = value;
            }
        }

		/// <summary>
		/// Constructor
		/// </summary>
        public Watchdog(/*Thread parentThread*/)
        {
			backgroundThread = null;
        }

        /// <summary>
        /// Start
        /// </summary>
		public void Start()
        {
			backgroundThread = new Thread(new ThreadStart(Ticker));
			backgroundThread.IsBackground = true;
			backgroundThread.Name = "Watchdog thread";
#if !SILVERLIGHT
            backgroundThread.Priority = ThreadPriority.Lowest;
#endif
			backgroundThread.Start();
        }

		/// <summary>
		/// Stop
		/// </summary>
		public void Stop()
        {
			backgroundThread.Abort();
			backgroundThread = null;
        }

		/// <summary>
		/// OnTick
		/// </summary>
		public event EventHandler OnTick;

        private void Ticker()
        {
            while (true)
            {
                OnTick(this, new EventArgs());
                Thread.Sleep(Period);
            }
        }

    }
}